package com.xc.erp.modules.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.erp.modules.erp.domain.pojo.JshRoleFunction;
import com.xc.erp.modules.erp.service.JshRoleFunctionService;
import com.xc.erp.modules.erp.mapper.JshRoleFunctionMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class JshRoleFunctionServiceImpl extends ServiceImpl<JshRoleFunctionMapper, JshRoleFunction>
    implements JshRoleFunctionService{

    /**
     * 分配权限
     * @param functionIds 功能编号集合
     * @param roleId 角色编号
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean addRoleAndFunction(Long[] functionIds, Long roleId) {
        List<JshRoleFunction> roleFunctionList = new ArrayList<>();
        for (Long functionId : functionIds) {
            if (ArrayUtils.isNotEmpty(functionIds)){
                //删除角色和功能表的关系
                baseMapper.delete(new QueryWrapper<JshRoleFunction>().eq("jsh_role_id",roleId).eq("jsh_function_id",functionId));
            }
            JshRoleFunction jshRoleFunction = new JshRoleFunction();
            jshRoleFunction.setJshRoleId(roleId);
            jshRoleFunction.setJshFunctionId(functionId);
            roleFunctionList.add(jshRoleFunction);
        }
        return saveBatch(roleFunctionList);
    }

    @Override
    public boolean removeByRoleId(Integer roleId) {
        return baseMapper.removeByRoleId(roleId);
    }

    @Override
    public boolean removeByFunctionId(Integer functionId) {
        return baseMapper.removeByFunctionId(functionId);
    }
}




